#Region " Imports "

Imports System
Imports System.Data
Imports System.IO
Imports System.Web.UI
Imports ICSharpCode.SharpZipLib.BZip2
Imports ICSharpCode.SharpZipLib.Zip
Imports ICSharpCode.SharpZipLib.Zip.Compression
Imports ICSharpCode.SharpZipLib.Zip.Compression.Streams
Imports ICSharpCode.SharpZipLib.GZip
Imports System.Xml
Imports BlazeApps.Library.Themes

#End Region

Partial Class Usercontrols_Admin_Themes
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Private ThemeGuid As Guid
    Private DataPageLocal As New PagedDataSource

    Public Property sortCriteria() As String
        Get
            Return CStr(ViewState("sortCriteria"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortCriteria") = Value
        End Set
    End Property

    Public Property sortDir() As String
        Get
            Return CStr(ViewState("sortDir"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortDir") = Value
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("Theme Management")

        GetQueryStringValues()

        If Me.Page.IsPostBack = False Then
            LoadPage()
        End If

        ConfigBaseNavCtrls()

        If Me.Page.IsPostBack = False Then
            Me.sortCriteria = "themename"
            Me.sortDir = "asc"

            BindDataItems()

            If Not ThemeGuid = Guid.Empty Then
                LoadEditData()
            End If

        End If

        Me.uxAdminPanel.Visible = ShowControls(SecurityMask.Admin, True)

    End Sub

#End Region

#Region " Save Data "

    Private Sub SaveTheme()

        If Not Directory.Exists(PhysicalAppPath & "App_Themes\" & Me.uxAddThemeFolderTextBox.Text.Trim.ToString) Then
            Directory.CreateDirectory(PhysicalAppPath & "App_Themes\" & Me.uxAddThemeFolderTextBox.Text.Trim.ToString)
            Directory.CreateDirectory(PhysicalAppPath & "Container\" & Me.uxAddThemeFolderTextBox.Text.Trim.ToString)

            Dim FilePathSource As String = String.Empty
            Dim FilePathDestination As String = String.Empty

            FilePathSource = String.Format("{0}Container", PhysicalAppPath)
            FilePathDestination = String.Format("{0}Container\{1}", PhysicalAppPath, Me.uxAddThemeFolderTextBox.Text.Trim.ToString)

            File.Copy(String.Format("{0}\ContainerTemplate.ascx", FilePathSource), String.Format("{0}\ContainerTemplate.ascx", FilePathDestination))

        End If

        If Path.GetExtension(Me.uxThemeFileUpload.PostedFile.FileName.ToString).ToUpper() = ".ZIP" Then
            ExtractZipFile(Me.uxThemeFileUpload, PhysicalAppPath & "App_Themes\" & Me.uxAddThemeFolderTextBox.Text.Trim.ToString)
        Else
            ScriptManager.RegisterStartupScript(uxSaveButton, Me.GetType(), "warning", "window.alert('Only ZIP files are allowed for uploading themes.');", True)
        End If

        Dim ThemeItem As ThemeDetail = ThemeDetail.NewThemeDetail

        ThemeItem.Themeid = Guid.NewGuid
        ThemeItem.Themename = Me.uxThemeNameTextBox.Text.Trim.ToString
        ThemeItem.Themefolder = Me.uxAddThemeFolderTextBox.Text.Trim.ToString
        ThemeItem.Save()

        BindDataItems()

    End Sub

    Private Sub UpdateTheme()
        Dim ThemeItem As ThemeDetail = ThemeDetail.GetThemeDetail(ThemeGuid)

        ThemeItem.Themeid = ThemeGuid
        ThemeItem.Themename = Me.uxThemeNameTextBox.Text.Trim.ToString
        ThemeItem.Themefolder = Me.uxAddThemeFolderTextBox.Text.Trim.ToString
        ThemeItem.Save()

        BindDataItems()
    End Sub

#End Region

#Region " Load Data "

    Private Sub LoadPage()
        LoadPageSize(Me.uxPageSizeDropDownList)
    End Sub

    Public Overloads Overrides Sub BindDataItems()
        Dim Themes As New ThemeList
        Dim DT As New DataTable
        Dim DV As New DataView

        DT = Themes.GetThemeList()

        Me.RecordCount = DT.Rows.Count

        DV.Table = DT

        DV.Sort = Me.sortCriteria & " " & Me.sortDir

        Me.DataPage.DataSource = DV
        Me.DataPage.AllowPaging = True
        Me.DataPage.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.DataPage.CurrentPageIndex = Me.PageNumber

        CreatePagingLinks()

        With Me.uxThemeGridView
            .DataSource = Me.DataPage
            .DataBind()
        End With
    End Sub

    Private Sub LoadEditData()
        Dim ThemeItem As ThemeDetail = ThemeDetail.GetThemeDetail(ThemeGuid)

        Me.uxThemeFileUpload.Enabled = False
        Me.uxAddThemeFolderTextBox.Enabled = False

        Me.uxThemeNameTextBox.Text = ThemeItem.Themename
        Me.uxAddThemeFolderTextBox.Text = ThemeItem.Themefolder
    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxSaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSaveButton.Click
        If Not ThemeGuid = Guid.Empty Then
            UpdateTheme()
        Else
            SaveTheme()
        End If
    End Sub

    Protected Sub uxResetButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxResetButton.Click
        Response.Redirect(String.Format("{0}.aspx?pid=adm_themes", AppPage))
    End Sub

    Protected Sub uxThemeGridView_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles uxThemeGridView.Sorting
        'Get the Sort by Value from the Sortexpression field on the column
        If Me.sortCriteria = e.SortExpression Then
            If Me.sortDir = "desc" Then
                Me.sortDir = "asc"
            Else
                Me.sortDir = "desc"
            End If
        End If

        ' Assign the column clicked to the sortCriteria property
        Me.sortCriteria = e.SortExpression

        'Calls the function to rebuild the datagrid view, with the new sort order
        BindDataItems()
    End Sub

#End Region

#Region " Helpers and Methods "

    Private Sub ConfigBaseNavCtrls()

        Me.PagingTopPanel = Me.uxPagingTopPanel
        Me.PagingBottomPanel = Me.uxPagingBottomPanel

        Me.RepeaterTop = Me.uxPagesTopRepeater
        Me.RepeaterBottom = Me.uxPagesBottomRepeater

        Me.NextTopLinkButton = Me.uxTopNextLinkButton
        Me.PreviousTopLinkButton = Me.uxTopPrevLinkButton
        Me.NextBottomLinkButton = Me.uxBottomNextLinkButton
        Me.PreviousBottomLinkButton = Me.uxBottomPrevLinkButton

        Me.DataPage = DataPageLocal
        Me.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.PagesToDisplay = 10

        Me.ItemsPerPageDropDown = Me.uxPageSizeDropDownList
        Me.ItemsPerPageLabel = Me.uxItemsPerPageLabel
        Me.PagerInfoLabel = Me.uxPagerInfoLabel

        AddHandler Me.RepeaterTop.ItemCommand, AddressOf PagingRepeater_ItemCommand
        AddHandler Me.RepeaterBottom.ItemCommand, AddressOf PagingRepeater_ItemCommand

    End Sub

    Private Sub GetQueryStringValues()
        ThemeGuid = GetGuidFromQueryString("themeid")
    End Sub

#End Region

End Class
